/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.DocData;
import com.openbrew.openbrewrf.Documents;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.documents")
public class DocumentsFacadeREST extends AbstractFacade<Documents> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public DocumentsFacadeREST() {
        super(Documents.class);
    }
    
    @POST
    @Override
    @WebMethod(operationName = "create")
    public Documents create(@WebParam(name = "documents") Documents entity){
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Documents entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Documents find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Path("{id}/get_pdf")
    @Produces({"application/pdf"})
    public StreamingOutput findPDF(@PathParam("id") Integer id) {
        final Documents doc = super.find(id);
        if(doc.getDocData()!=0){
            final DocData dd = getDocData(doc.getDocData(), em);
            return new StreamingOutput() {
                @Override
                public void write(OutputStream output) throws IOException, WebApplicationException {
                    try {
                        byte[] bytes = dd.getDoc();
                        output.write(bytes);
                    } catch (IOException e) {
                        throw new WebApplicationException(e);
                    }
                }
            };
        } else {
            return null;
        }
    }
    
    @GET
    @Path("{id}/get_gen")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getFile(@PathParam("id") Integer id) {
        final Documents doc = super.find(id);
        if(doc.getDocData()!=0){
            final DocData dd = getDocData(doc.getDocData(), em);
            return Response.ok(dd.getDoc(), MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-Disposition", "attachment; filename=\"" + doc.getDocDescription() + "\"" ) 
                .build();
        } 
        return null;
    }
    
    /***************************************************************************
     * Database modification functions.
     **************************************************************************/
    private DocData getDocData(final int id, final EntityManager en){
        TypedQuery<DocData> query = en.createQuery(
            "SELECT d FROM DocData d WHERE d.ddid = ?1", DocData.class);
        query.setParameter(1, id);
        DocData dd = query.getSingleResult();
        return dd;
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Documents> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Documents> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
